home *** CD-ROM | disk | FTP | other *** search
- // Main.c - Does the application initialization, event loop
- // and high-level interface maintenance
-
- #include "CTB.h"
- #include "Game.h"
-
- // event masks
- #define kResumeMask 0x010000000
-
- // mbar
- #define rMBarID 256
-
- // menus
- #define rAppleMenuID 256
- #define rFileMenuID 257
- #define rEditMenuID 258
- #define rTestMenuID 260
- // see also CTB.h
-
- // menu items
- #define rAboutMItem 1
- #define rNewGameItem 1
- #define rQuitMItem 3
- #define rTryMItem 1
-
- // about alert
- #define rAboutAlertID 128
-
- // cursors
- #define rHammerCurs 128
-
- // globals
- Handle gMBar;
- MenuHandle gAppleMenuH, gFileMenuH, gEditMenuH, gConnMenuH;
- WindowPtr gGameWindow;
- CursHandle gHammerHand;
- Boolean gGameInProgress, gOnline;
-
- //**********************************************************
- //
- // ExitNow - Does stuff that should happen before ExitToShell
- //
- //**********************************************************
- pascal void ExitNow(void)
- {
- DisposeConn(); // close connection-related stuff
- ExitToShell();
- }
-
- //**********************************************************
- //
- // DoQuit - Causes a normal exit
- //
- //**********************************************************
- pascal void DoQuit(void)
- {
- DisposeConn(); // close connection-related stuff
- if (gGameInProgress)
- ShowScores();
- ExitToShell();
- }
-
- //**********************************************************
- //
- // InitSys - Initializes the usual system stuff
- //
- //**********************************************************
- void InitSys(void)
- {
- InitGraf(&qd.thePort);
- InitWindows();
- InitFonts();
- InitMenus();
- InitDialogs(ExitNow);
- InitCursor();
- FlushEvents(everyEvent, nil);
- }
-
- //**********************************************************
- //
- // SetupMenus - Installs our menus
- //
- //**********************************************************
- void SetupMenus(void)
- {
- gMBar = GetNewMBar(rMBarID);
- SetMenuBar(gMBar);
- gAppleMenuH = GetMHandle(rAppleMenuID);
- gFileMenuH = GetMHandle(rFileMenuID);
- gEditMenuH = GetMHandle(rEditMenuID);
- gConnMenuH = GetMHandle(rConnMenuID);
- AddResMenu(gAppleMenuH, 'DRVR');
- DrawMenuBar();
- }
-
- //**********************************************************
- //
- // InitApp - Initializes application-specific stuff
- //
- //**********************************************************
- void InitApp(void)
- {
- SetupMenus();
- gHammerHand = nil;
- if (gHammerHand = GetCursor(rHammerCurs)) {
- MoveHHi((Handle)gHammerHand);
- HLock((Handle)gHammerHand);
- }
- if (NewConn(kAsync, kDoInitCTB, kDontPrompt)) {
- SysBeep(1);
- ExitNow(); // you will want to report this error
- }
- gGameWindow = NewGameWindow(); // create game window
- gOnline = false; // initialize variables tracked here
- gGameInProgress = false;
- }
-
- //**********************************************************
- //
- // DoMenu - Handles a selection
- //
- //**********************************************************
- void DoMenu(long menuResult)
- {
- short menuID, itemNumber;
- Str255 daName;
- GrafPtr oldPort;
-
- menuID = HiWord(menuResult);
- itemNumber = LoWord(menuResult);
-
- switch(menuID) {
- case rAppleMenuID:
- if (itemNumber == rAboutMItem) {
- SetCursor(&qd.arrow);
- Alert(rAboutAlertID, nil);
- } else {
- GetItem(gAppleMenuH, itemNumber, daName);
- GetPort(&oldPort);
- OpenDeskAcc(daName);
- SetPort(oldPort);
- }
- break;
- case rFileMenuID:
- switch(itemNumber) {
- case rNewGameItem:
- if (SendNewGame())
- SysBeep(1); // a reportable error error
- else
- NewGame(); // report errors in these calls?
- gGameInProgress = true;
- break;
- case rQuitMItem:
- DoQuit();
- break;
- }
- break;
- case rEditMenuID:
- SystemEdit(itemNumber - 1);
- break;
- case rConnMenuID: // CTB.c maintains this menu
- DoConnMenu(itemNumber);
- break;
- default:
- break;
- }
- HiliteMenu(0);
- }
-
- //**********************************************************
- //
- // AdjustCursor - Make sure the hammer showing if necessary
- //
- //**********************************************************
- void AdjustCursor(Point thePoint, RgnHandle theRgn)
- {
- if (PtInRgn(thePoint, ((WindowPeek)gGameWindow)->contRgn)) {
- if (gGameInProgress)
- SetCursor(*gHammerHand);
- else
- SetCursor(&qd.arrow);
- CopyRgn(((WindowPeek)gGameWindow)->contRgn, theRgn);
- } else {
- SetCursor(&qd.arrow);
- DiffRgn(GetGrayRgn(),
- ((WindowPeek)gGameWindow)->contRgn, theRgn);
- }
- }
-
- //**********************************************************
- //
- // AdjustMenus - Adjust any menus that change on context
- //
- //**********************************************************
- void AdjustMenus(void)
- {
- if (gOnline)
- EnableItem(gFileMenuH, rNewGameItem);
- else
- DisableItem(gFileMenuH, rNewGameItem);
- }
-
- //**********************************************************
- //
- // CurMousePoint - Find mouse position in global coordinates
- //
- //**********************************************************
- Point CurMousePoint(void)
- {
- Point p;
-
- GetMouse(&p);
- GlobalToLocal(&p);
-
- return p;
- }
-
- //**********************************************************
- //
- // main - The obligatory big unit
- //
- //**********************************************************
- void main(void)
- {
- EventRecord theEvent;
- WindowPtr whichWindow;
- Boolean gameWasInProgress;
- RgnHandle mouseRgn;
-
- InitSys();
- InitApp();
-
- gameWasInProgress = false; // initialize status-change sniffer
- mouseRgn = NewRgn(); // allocate region for cursor maintenance
-
- while (true) {
-
- // if game ends, kill hammer cursor, show scores
- if (gGameInProgress != gameWasInProgress)
- AdjustCursor(CurMousePoint(), mouseRgn); // force cursor
- if (gameWasInProgress && !gGameInProgress) // re-evaluation
- ShowScores();
-
- gameWasInProgress = gGameInProgress;
-
- IdleConn(&gOnline); // give connection manager time
- if (!gOnline) // can't be in progress if offline
- gGameInProgress = false;
- IdleGame(&gGameInProgress); // give time to deal with messages
- AdjustMenus();
-
- // main event loop - all the usual stuff
- if (WaitNextEvent(everyEvent, &theEvent, 0, mouseRgn))
- if (!ConnEvent(&theEvent)) { // give tool chance to handle
- switch (theEvent.what) {
- case activateEvt: // pass event to tool
- ConnActivate((WindowPtr)theEvent.message,
- theEvent.modifiers & activeFlag);
- break;
- case updateEvt:
- if ((WindowPtr)theEvent.message == gGameWindow)
- UpdateGameWindow();
- AdjustCursor(CurMousePoint(), mouseRgn);
- break; // force re-evaluation of cursor
- case keyDown:
- case autoKey:
- if (theEvent.modifiers & cmdKey)
- DoMenu(MenuKey(theEvent.message &
- charCodeMask));
- break;
- case mouseDown:
- switch (FindWindow(theEvent.where,
- &whichWindow)) {
- case inMenuBar:
- DoMenu(MenuSelect(theEvent.where));
- break;
- case inSysWindow:
- SystemClick(&theEvent, whichWindow);
- break;
- case inGoAway:
- if (TrackGoAway(whichWindow, theEvent.where))
- DoQuit();
- break;
- case inDrag:
- DragWindow(whichWindow, theEvent.where,
- &qd.screenBits.bounds);
- break;
- case inContent:
- if (gGameInProgress)
- HandleWindowClick(theEvent.where,
- &gGameInProgress);
- break;
- default:
- break;
- }
- break;
- case osEvt: // Allow tool to handle resume event
- if (theEvent.message & kResumeMask)
- ConnResume(FrontWindow(), theEvent.message &
- suspendResumeMessage);
- AdjustCursor(theEvent.where, mouseRgn);
- break; // Adjust for all OS Events
- } // (not just mouseMoved)
- }
- }
- }